// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
	"project_id": {
		"rules": [{
				"required": true
			},
			{
				"format": "string"
			}
		],
		"title": "项目名称",
		"label": "项目名称"
	},
	"place_id": {
		"rules": [{
				"required": true
			},
			{
				"format": "string"
			}
		],
		"title": "门店信息",
		"label": "门店信息"
	},
	"date": {
		"rules": [{
			"required": true
		}, {
			"format": "array"
		}],
		"title": "项目时间",
		"label": "项目时间"
	},
	"status": {
		"rules": [{
				"format": "int"
			},
			{
				"range": [{
						"text": "可抢单",
						"value": 0
					},
					{
						"text": "已被抢",
						"value": 1
					}
				]
			}
		],
		"title": "抢单状态",
		"defaultValue": 0,
		"label": "抢单状态"
	},
	"all_physios": {
		"rules": [{
			"format": "array"
		}],
		"title": "指定抢单人员",
		"label": "指定抢单人员"
	},
	"winner": {
		"rules": [{
			"format": "string"
		}],
		"title": "抢单康复师(不指定则抢单模式)",
		"label": "抢单康复师(不指定则抢单模式)"
	}
}

const enumConverter = {
	"status_valuetotext": {
		"0": "可抢单",
		"1": "已被抢"
	}
}

function filterToWhere(filter, command) {
	let where = {}
	for (let field in filter) {
		let {
			type,
			value
		} = filter[field]
		switch (type) {
			case "search":
				if (typeof value === 'string' && value.length) {
					where[field] = new RegExp(value)
				}
				break;
			case "select":
				if (value.length) {
					let selectValue = []
					for (let s of value) {
						selectValue.push(command.eq(s))
					}
					where[field] = command.or(selectValue)
				}
				break;
			case "range":
				if (value.length) {
					let gt = value[0]
					let lt = value[1]
					where[field] = command.and([command.gte(gt), command.lte(lt)])
				}
				break;
			case "date":
				if (value.length) {
					let [s, e] = value
					let startDate = new Date(s)
					let endDate = new Date(e)
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
			case "timestamp":
				if (value.length) {
					let [startDate, endDate] = value
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
		}
	}
	return where
}

export {
	validator,
	enumConverter,
	filterToWhere
}